/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gisgraph.components;

import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.io.GraphIOException;
import edu.uci.ics.jung.io.GraphMLWriter;
import edu.uci.ics.jung.io.graphml.EdgeMetadata;
import edu.uci.ics.jung.io.graphml.GraphMLReader2;
import edu.uci.ics.jung.io.graphml.GraphMetadata;
import edu.uci.ics.jung.io.graphml.HyperEdgeMetadata;
import edu.uci.ics.jung.io.graphml.NodeMetadata;
import gisgraph.GISGraphView;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
import org.apache.commons.collections15.Transformer;

/**
 *
 * @author Slawek
 */
public class GraphHelper {

    public static void write(DirectedSparseGraph<GISVertex, GISEdge> g, String filename)
            throws Exception {
        Writer output = null;

        File file = new File(filename);
        output = new BufferedWriter(new FileWriter(file));

        GraphMLWriter writer = new GraphMLWriter();

        Transformer<GISEdge, String> transformer = new Transformer<GISEdge, String>() {
            public String transform(GISEdge e) {
                return new Boolean(e.getIsReliable()).toString();
            }
        };
        
        Transformer<GISEdge, String> weightTransformer = new Transformer<GISEdge, String>() {
            public String transform(GISEdge e) {
                return ((Integer)e.getWeight()).toString();
            }
        };
        
        Transformer<GISVertex, String> spTransformer = new Transformer<GISVertex, String>() {
            public String transform(GISVertex i) {
                return ((Boolean)i.getIsStartPoint()).toString();
            }
        };
                
        Transformer<GISVertex, String> epTransformer = new Transformer<GISVertex, String>() {
            public String transform(GISVertex i) {
                return ((Boolean)i.getIsEndPoint()).toString();
            }
        };

        writer.addEdgeData("IsReliable", "Czy ścieżka jest niezawodna", "false", transformer);
        writer.addEdgeData("Weight", "Waga ścieżki", "false", weightTransformer);
        writer.addVertexData("IsEndPoint", "Czy pkt końcowy", "false", epTransformer);
        writer.addVertexData("IsStartPoint", "Czy pkt początkowy", "false", spTransformer);
        writer.save(g, output);

        //output.write(text);
        output.close();
    }

    public static DirectedSparseGraph<GISVertex, GISEdge> read(String filename, GISGraphView gView) {
        
        
        DirectedSparseGraph<GISVertex, GISEdge> graph =
                new DirectedSparseGraph<GISVertex, GISEdge>();

        Reader reader;

        try {
            reader = new FileReader(filename);
        } catch (FileNotFoundException ex) {
            gView.ConsoleWriteLine(ex.getMessage());
            return graph;
        }

        Transformer<NodeMetadata, GISVertex> vtrans = new Transformer<NodeMetadata, GISVertex>() {
            public GISVertex transform(NodeMetadata nmd) {
                Integer id = Integer.valueOf(nmd.getId());
                boolean isStartPoint = Boolean.valueOf(nmd.getProperty("IsStartPoint"));
                boolean isEndPoint = Boolean.valueOf(nmd.getProperty("IsEndPoint"));
                return new GISVertex((int) id, isStartPoint, isEndPoint);
            }
        };

        Transformer<EdgeMetadata, GISEdge> etrans = new Transformer<EdgeMetadata, GISEdge>() {

            public GISEdge transform(EdgeMetadata emd) {
                boolean isReliable = Boolean.parseBoolean(emd.getProperty("IsReliable"));
                int weight = Integer.parseInt(emd.getProperty("Weight"));
                GISEdge e = new GISEdge(isReliable, weight);
                return e;
            }
        };
        
        Transformer<HyperEdgeMetadata, GISEdge> hetrans = new Transformer<HyperEdgeMetadata, GISEdge>() {

            public GISEdge transform(HyperEdgeMetadata emd) {
                boolean isReliable = Boolean.parseBoolean(emd.getProperty("IsReliable"));
                int weight = Integer.parseInt(emd.getProperty("Weight"));
                GISEdge e = new GISEdge(isReliable, weight);
                return e;
            }
        };
        
        Transformer<GraphMetadata, DirectedSparseGraph> gtrans = new Transformer<GraphMetadata, DirectedSparseGraph>() {

            public DirectedSparseGraph<Integer, GISEdge> transform(GraphMetadata gmd) {
                return new DirectedSparseGraph<Integer, GISEdge>();
            }
        };

        GraphMLReader2<DirectedSparseGraph<GISVertex, GISEdge>, GISVertex, GISEdge> gmlr =
                new GraphMLReader2(
                reader,
                gtrans,
                vtrans,
                etrans,
                hetrans);
        try {
            graph = gmlr.readGraph();
        } catch (GraphIOException ex) {
            gView.ConsoleWriteLine(ex.getMessage());
            return graph;
        }

        return graph;
    }
}
